package demo5_p;

public class Test {

    //public static void func(Animal a){
    //}
    public static Animal func2(){
        Dog dog = new Dog("旺财", 10);
        return dog;
    }
    public static void main1(String[] args) {
        /*Dog dog = new Dog("旺财", 10);
        Animal animal = dog;*/
        //向上转型，父类引用子类对象
        //直接赋值
        Animal animal = new Dog("旺财", 10);
        //方法传参
        Dog dog = new Dog("旺财", 10);
        func(dog);
        //返回值
        Animal animal2 = func2();
    }

    public static void main2(String[] args) {
        Dog dog = new Dog("旺财", 10);
        dog.bark();

        //报错，因为父类没有bark方法（不能通过父类引用调用子类方法）
        Animal animal = new Dog("旺财", 10);
        //animal.bark();
        animal.eat();//这里通过父类引用调用了子类方法//重写，动态绑定
    }

    public  static  void func(Animal animal){
        animal.eat();
    }

    public static void main3(String[] args) {
        Dog dog = new Dog("旺财", 10);
        func(dog);
        Bird bird = new Bird("小鸽", 1);
        func(bird);
    }

    public static void main(String[] args) {
        Animal aniamal = new Bird("小鸽", 1);
        //向下转型
        Bird bird = (Bird) aniamal;
        bird.fly();
        Animal animal = new Dog("单身狗", 10);
        //判断animal引用是否是Bird
        if(animal instanceof Bird){
            Bird bird2 = (Bird) animal;
            bird2.fly();
        }else{
            System.out.println("不是小鸽");
        }
    }
}
